package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.redefinableElement.feature.property;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.uml2.common.edit.command.SubsetAddCommand;
import org.eclipse.uml2.common.edit.command.SubsetSupersetReplaceCommand;
import org.eclipse.uml2.common.edit.command.SubsetSupersetSetCommand;
import org.eclipse.uml2.common.edit.command.SupersetRemoveCommand;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;
import rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.redefinableElement.feature.StructuralFeatureItemProvider;

public class PropertyItemProvider
		extends StructuralFeatureItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public PropertyItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addOwningTemplateParameterPropertyDescriptor(object);
			addTemplateParameterPropertyDescriptor(object);
			addEndPropertyDescriptor(object);
			addDeploymentPropertyDescriptor(object);
			addDeployedElementPropertyDescriptor(object);

			addClass_PropertyDescriptor(object);
			addDatatypePropertyDescriptor(object);
			addIsDerivedPropertyDescriptor(object);
			addIsDerivedUnionPropertyDescriptor(object);
			addDefaultPropertyDescriptor(object);
			addAggregationPropertyDescriptor(object);
			addIsCompositePropertyDescriptor(object);
			addRedefinedPropertyPropertyDescriptor(object);
			addOwningAssociationPropertyDescriptor(object);
			addDefaultValuePropertyDescriptor(object);
			addOppositePropertyDescriptor(object);
			addSubsettedPropertyPropertyDescriptor(object);
			addAssociationPropertyDescriptor(object);
			addQualifierPropertyDescriptor(object);
			addAssociationEndPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	protected void addOwningTemplateParameterPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_ParameterableElement_owningTemplateParameter_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_ParameterableElement_owningTemplateParameter_feature", "_UI_ParameterableElement_type"),
				UMLPackage.Literals.PARAMETERABLE_ELEMENT__OWNING_TEMPLATE_PARAMETER, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addTemplateParameterPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_ParameterableElement_templateParameter_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_ParameterableElement_templateParameter_feature", "_UI_ParameterableElement_type"),
				UMLPackage.Literals.PARAMETERABLE_ELEMENT__TEMPLATE_PARAMETER, true, null, null, null));
	}

	protected void addEndPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_ConnectableElement_end_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_ConnectableElement_end_feature", "_UI_ConnectableElement_type"),
				UMLPackage.Literals.CONNECTABLE_ELEMENT__END, true, null, null, null));
	}

	protected void addDeploymentPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_DeploymentTarget_deployment_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_DeploymentTarget_deployment_feature", "_UI_DeploymentTarget_type"),
				UMLPackage.Literals.DEPLOYMENT_TARGET__DEPLOYMENT, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addDeployedElementPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_DeploymentTarget_deployedElement_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_DeploymentTarget_deployedElement_feature", "_UI_DeploymentTarget_type"),
				UMLPackage.Literals.DEPLOYMENT_TARGET__DEPLOYED_ELEMENT, false, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addTemplateBindingPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_TemplateableElement_templateBinding_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_TemplateableElement_templateBinding_feature", "_UI_TemplateableElement_type"),
				UMLPackage.Literals.TEMPLATEABLE_ELEMENT__TEMPLATE_BINDING, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addOwnedTemplateSignaturePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_TemplateableElement_ownedTemplateSignature_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_TemplateableElement_ownedTemplateSignature_feature", "_UI_TemplateableElement_type"),
				UMLPackage.Literals.TEMPLATEABLE_ELEMENT__OWNED_TEMPLATE_SIGNATURE, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addClass_PropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_class__feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_class__feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__CLASS, false, null, null, null));
	}

	protected void addDatatypePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_datatype_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_datatype_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__DATATYPE, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addIsDerivedPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_isDerived_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_isDerived_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__IS_DERIVED, true, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, null));
	}

	protected void addIsDerivedUnionPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_isDerivedUnion_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_isDerivedUnion_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__IS_DERIVED_UNION, true, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, null));
	}

	protected void addDefaultPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_default_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_default_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__DEFAULT, true, ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, null));
	}

	protected void addAggregationPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_aggregation_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_aggregation_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__AGGREGATION, true, ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, null));
	}

	protected void addIsCompositePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_isComposite_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_isComposite_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__IS_COMPOSITE, true, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addRedefinedPropertyPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_redefinedProperty_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_redefinedProperty_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__REDEFINED_PROPERTY, true, null, null, null));
	}

	protected void addOwningAssociationPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_owningAssociation_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_owningAssociation_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__OWNING_ASSOCIATION, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addDefaultValuePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_defaultValue_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_defaultValue_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__DEFAULT_VALUE, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addOppositePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_opposite_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_opposite_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__OPPOSITE, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addSubsettedPropertyPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_subsettedProperty_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_subsettedProperty_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__SUBSETTED_PROPERTY, true, null, null, null));
	}

	protected void addAssociationPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_association_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_association_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__ASSOCIATION, true, null, null, null));
	}

	protected void addQualifierPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_qualifier_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_qualifier_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__QUALIFIER, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addAssociationEndPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Property_associationEnd_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Property_associationEnd_feature", "_UI_Property_type"),
				UMLPackage.Literals.PROPERTY__ASSOCIATION_END, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	public Collection getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures.add(UMLPackage.Literals.DEPLOYMENT_TARGET__DEPLOYMENT);

			childrenFeatures.add(UMLPackage.Literals.PROPERTY__DEFAULT_VALUE);
			childrenFeatures.add(UMLPackage.Literals.PROPERTY__QUALIFIER);
		}
		return childrenFeatures;
	}

	protected EStructuralFeature getChildFeature(Object object, Object child) {

		return super.getChildFeature(object, child);
	}

	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/Property"));
	}

	public String getText(Object object) {
		final StringBuffer text = appendType(appendKeywords(new StringBuffer(), object), "_UI_Property_type");

		final Property property = (Property) object;
		final String label = property.getLabel(shouldTranslate());

		if (label != null && label.length() > 0) {
			appendString(text, label);
			final Type type = property.getType();

			if (null != type) {
				final String typeName = type.getName();

				if (typeName != null && typeName.length() > 0) {
					appendString(text, ": " + typeName);
				}
			}
		} else if (null != property.getAssociation()) {
			final Type type = property.getType();

			if (null != type) {
				final String typeName = type.getName();

				if (typeName.length() > 0) {
					appendString(text, Character.toLowerCase(typeName.charAt(0)) + typeName.substring(1));
				}
			}
		}

		if (property.isMultivalued()) {
			text.append("[]");
		}
		return text.toString();
	}

	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(Property.class)) {
			case UMLPackage.PROPERTY__IS_DERIVED:
			case UMLPackage.PROPERTY__IS_DERIVED_UNION:
			case UMLPackage.PROPERTY__DEFAULT:
			case UMLPackage.PROPERTY__AGGREGATION:
			case UMLPackage.PROPERTY__IS_COMPOSITE:
			case UMLPackage.PROPERTY__ASSOCIATION:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
			case UMLPackage.PROPERTY__DEPLOYMENT:

			case UMLPackage.PROPERTY__DEFAULT_VALUE:
			case UMLPackage.PROPERTY__QUALIFIER:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

	}

	public String getCreateChildText(Object owner, Object feature, Object child, Collection selection) {
		final Object childFeature = feature;
		final Object childObject = child;

		final boolean qualify = childFeature == UMLPackage.Literals.NAMED_ELEMENT__NAME_EXPRESSION || childFeature == UMLPackage.Literals.MULTIPLICITY_ELEMENT__UPPER_VALUE || childFeature == UMLPackage.Literals.MULTIPLICITY_ELEMENT__LOWER_VALUE
				|| childFeature == UMLPackage.Literals.PROPERTY__DEFAULT_VALUE;

		if (qualify) {
			return getString("_UI_CreateChild_text2",
					new Object[] { getTypeText(childObject), getFeatureText(childFeature), getTypeText(owner) });
		}
		return super.getCreateChildText(owner, feature, child, selection);
	}

	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

	protected Command createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection, int index) {
		if (feature == UMLPackage.Literals.DEPLOYMENT_TARGET__DEPLOYMENT) {
			return new SubsetAddCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.NAMED_ELEMENT__CLIENT_DEPENDENCY }, collection, index);
		}
		return super.createAddCommand(domain, owner, feature, collection, index);
	}

	protected Command createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection collection) {
		if (feature == UMLPackage.Literals.NAMED_ELEMENT__CLIENT_DEPENDENCY) {
			return new SupersetRemoveCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.DEPLOYMENT_TARGET__DEPLOYMENT }, collection);
		}
		return super.createRemoveCommand(domain, owner, feature, collection);
	}

	protected Command createReplaceCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, EObject value, Collection collection) {
		if (feature == UMLPackage.Literals.DEPLOYMENT_TARGET__DEPLOYMENT) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.NAMED_ELEMENT__CLIENT_DEPENDENCY }, null, value, collection);
		}
		if (feature == UMLPackage.Literals.NAMED_ELEMENT__CLIENT_DEPENDENCY) {
			return new SubsetSupersetReplaceCommand(domain, owner, feature, null, new EStructuralFeature[] { UMLPackage.Literals.DEPLOYMENT_TARGET__DEPLOYMENT }, value, collection);
		}
		return super.createReplaceCommand(domain, owner, feature, value, collection);
	}

	protected Command createSetCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value) {
		if (feature == UMLPackage.Literals.PARAMETERABLE_ELEMENT__OWNING_TEMPLATE_PARAMETER) {
			return new SubsetSupersetSetCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.PARAMETERABLE_ELEMENT__TEMPLATE_PARAMETER }, null, value);
		}
		if (feature == UMLPackage.Literals.PROPERTY__OWNING_ASSOCIATION) {
			return new SubsetSupersetSetCommand(domain, owner, feature, new EStructuralFeature[] { UMLPackage.Literals.PROPERTY__ASSOCIATION }, null, value);
		}
		if (feature == UMLPackage.Literals.PROPERTY__ASSOCIATION) {
			return new SubsetSupersetSetCommand(domain, owner, feature, null, new EStructuralFeature[] { UMLPackage.Literals.PROPERTY__OWNING_ASSOCIATION }, value);
		}
		return super.createSetCommand(domain, owner, feature, value);
	}

}
